﻿define(['app'], function (app) {
    app.controller('selectBookController', ['$scope', '$rootScope', '$timeout', '$http', '$q', 'infoService', '$state', 'ngTableParams', '$uibModal', '$uibModalInstance',
       function ($scope, $rootScope, $timeout, $http, $q, infoService, $state, ngTableParams, $uibModal, $uibModalInstance) {

           //代码书写步骤
           //1.用【$scope】定义公共类model,和页面绑定的model
           //2.用【var】定义controller内部用的变量
           //3.用【$scope】和【var】定义方法，注意区分两者定义的方法的区别
           //4.最后定义【init】初始化方法，并调用，进行页面的初始化加载
           //定义规则，按可用性分忧优先级，公共的在前，private的在后

           //初始化listModel
           $scope.tableListData = [];
           $scope.filter = {};
           $scope.model = {};

           $scope.getFirst = function (params) {
               if (params.total() <= (params.page() - 1) * params.count()) {
                   return Math.floor(params.total() / params.count()) * params.count() + 1;
               }
               else {
                   return (params.page() - 1) * params.count() + 1;
               }
           };

           /**
            * 查询tableList数据
            * @return {[object]} [返回table列表数据]
            */
           $scope.tableParams = new ngTableParams({ sorting: { 'id': 'desc' } }, {
               getData: function (params) {
                   // $scope.global.isBusy = true;//暂时没有
                   if ($scope.filter.name == "") {
                       $scope.filter.name = null;
                   }
                   if ($scope.filter.author == "") {
                       $scope.filter.author = null;
                   }
                   if ($scope.filter.isbn == "") {
                       $scope.filter.isbn = null;
                   }
                   if ($scope.filter.language == "") {
                       $scope.filter.language = null;
                   }
                   if ($scope.filter.publisher == "") {
                       $scope.filter.publisher = null;
                   }
                   
                   return infoService.getbookAll().then(
                       function (response) {
                           //console.log(response);
                           //// $scope.global.isBusy = false;
                           //if (response.success) {
                           if (response.data && response.data.total) {
                               params.total(response.data.total);
                           }

                           $scope.first = $scope.getFirst(params);
                           var list = response.data;
                           //if (list != null) {
                           //    for (var i = 0; i < list.length; i++) {
                           //        list[i].cTime = new Date(list[i].cTime);
                           //        list[i].uTime = new Date(list[i].uTime);
                           //    }
                           //}
                           return list;
                           //} else {
                           //    console.log(response);
                           //    alert("查询数据失败!");
                           //}
                       },
                       function (code) {
                           alert("系统出现问题了！");
                       });
               
                   //return infoService.searchbook($scope.filter).then(
                   //    function (response) {
                   //        //console.log(response);
                   //        //// $scope.global.isBusy = false;
                   //        //if (response.success) {
                   //        if (response.data && response.data.total) {
                   //            params.total(response.data.total);
                   //        }

                   //        $scope.first = $scope.getFirst(params);
                   //        var list = response.data.list;
                   //        //if (list != null) {
                   //        //    for (var i = 0; i < list.length; i++) {
                   //        //        list[i].cTime = new Date(list[i].cTime);
                   //        //        list[i].uTime = new Date(list[i].uTime);
                   //        //    }
                   //        //}
                   //        return list;
                   //        //} else {
                   //        //    console.log(response);
                   //        //    alert("查询数据失败!");
                   //        //}
                   //    },
                   //    function (code) {
                   //        alert("系统出现问题了！");
                   //    });
               }
           });

           $scope.reflash = function () {
               $scope.tableParams.page(1);
               $scope.tableParams.reload();
           };

           $scope.select = function (item) {
               // $scope.global.isBusy = true;//暂时没有
               $uibModalInstance.dismiss(item);
           };
           $scope.cancel = function () {
               $uibModalInstance.dismiss("取消");
           };
       }]);

});